var jsonDataArr={};
var saleryTypeVal ={};
var rtoDocumentval ={};
var modeOfPaymentval ={};
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (day) + "-" + (month) + "-" + now.getFullYear();
$(document).ready(function(){
	
	
	
	
	window.alert = function() {};

	alert = function() {};
	
	
	
	$( "#dateOfBirth" ).datepicker({dateFormat : 'dd-mm-yy'});
	$( "#VahicalDate" ).datepicker({dateFormat : 'dd-mm-yy'});
	$( "#date_of_emi" ).datepicker({dateFormat : 'dd-mm-yy'});
	$( "#FinalDate" ).datepicker({dateFormat : 'dd-mm-yy'});
		
	$( "#dateOfBirth" ).val(today);
	$( "#VahicalDate" ).val(today);
	$( "#date_of_emi" ).val(today);
	$( "#FinalDate" ).val(today);
	
	
});
$(document).on('click', '#btnSubmit', function(e){
	
	
	$(".saveValue").each(function(){
		
		var inputID= $(this).attr("id");
		var name = $(this).attr("name");
		var value = $(this).val();
		
		
		
		
		/*$(":input[type=checkbox]").each(function(){
			
			if($("this").is(":checked")){
				jsonDataArr [name]=value;
			}
		});*/
		
		jsonDataArr [name]=value;
		
	});
	$(".SexClass").each(function(){
		var name = $(this).attr("name");
		if($(this).is(":checked")){
			
			jsonDataArr [name]=$(this).val();
		}
	});
	
	$(".saleryType").each(function e(){
		
		var name = $(this).attr("name");
		if($(this).is(":checked")){
			saleryTypeVal[name] = true;
			
			
		}else{
			saleryTypeVal[name] = false;
		}
		
	});
	jsonDataArr ["Type_Salary"]=JSON.stringify(saleryTypeVal);
	
	
	$(".rtoDocument").each(function e(){
		
		var name = $(this).attr("name");
		
		var name = $(this).attr("name");
		if($(this).is(":checked")){
			rtoDocumentval[name] = true;
			
			
		}else{
			rtoDocumentval[name] = false;
		}
		
		
	});
	jsonDataArr ["DOCUMENT_RTO_PASSING"]=JSON.stringify(rtoDocumentval);
	
	$(".modeOfPayment").each(function (e){
		
		var name = $(this).attr("name");
		if($(this).is(":checked")){
			modeOfPaymentval[name] = true;
			
			
		}else{
			modeOfPaymentval[name] = false;
		}
		/*var name = $(this).attr("name");
		if($(this).is(":checked")){
			
			if(modeOfPaymentval != ""){
				modeOfPaymentval = modeOfPaymentval + "," +name;	
			}else{
				modeOfPaymentval = name;
			}
			
		}*/
		
	});
	jsonDataArr ["Mode_Of_Payment"]= JSON.stringify(modeOfPaymentval);
	
	console.log("the Fucking data  arr",jsonDataArr);
	saveData(jsonDataArr);
	e.preventDefault(); 
});
$(document).on('change', 'input[type=file]', function(e){
	$in = $(this);
	var selFile = $in.val();

	var imageRegex = /([^\s]+(?=\.(jpg|gif|png|bmp))\.\2)/gm;
	if (selFile != null && selFile.toLowerCase().match(imageRegex)) {

		$(this).submit();
	} else if(selFile == null) {
		alert("Please select file to upload");
							
	}else {
		alert("Please select propert images file to upload");
	}
});

$(document).on("submit","#imageForm",function(e) {
	var options =
	{
		// target: '#status',
		dataType : "json",
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		/*
		 * url:
		 * "LoadSettingController?scubeAppNameLable="+scubeAppNameLable+"&r="+Math.random()+"&origCompId="+origCompId,
		 */
		url : "userImage?r=" + Math.random(),
		// url: "FileUpload?wixUserId=" + instanceId +
		// "&rowId="
		// + rowId + "&upload=" + Math.random(),
		success : function(xhr) {
			// alert('Thanks for your comment success!'
			// +
			// xhr.responseText);
			// alert('Thanks for your comment!');

		},
		error : function(xhr) {
			alert('Thanks for your comment!');
		},
		complete : function(xhr) {
			var data = xhr.responseText;
			console.log(data);
			
			$("#applicantImg").attr("src",data);
			jsonDataArr["applicantImgurl"] = data;
		}
	};

$(this).ajaxSubmit(options);
$("#appliImgUploadBtn").val("");
return false;
});

function saveData(dataArr){
	
	$.ajax({
		 url: "formDataAction?r="+Math.random(),
	     type: "POST",
	     data: {
	    	 	"dataToSave":JSON.stringify(dataArr)
	    },
	     success: function(data){
	    	 console.log("saveSetting",data);
	    	 
	    	 data = $.trim(data);
	    	 if(data =="savedSuccess"){
	    		 $("#applFormContainrTabel").hide();
	    		 $("#detailTd").empty();
	    		 $("#detailTd").append("<br> Data Saved Successfully <a href='satvik_login.html'>" +
	    		 		"please click here to add more</a><br>");
	    		 $("#detailTd").show();
	    		// window.location = "satvik_login.html";
	    	 }else{
	    		 
	    	 }
	    	 
	     },
	     error: function () {
	    	 console.log("here error");
	    	// sessionStorage.setItem("userlogin"+origCompId,"");
	     },async:false,
		
	});
	
	
}